﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text.RegularExpressions;

public partial class Category_UserControl_uc_Cate_ChiPhi : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
        }
    }
    private void InitData()
    {
        GetCostGroup();
        ClearTextBox();        
        ZD_CATE_COST obj = new ZD_CATE_COST();
        obj.NAME = "";
        obj.MA = "";
        obj.GHICHU = "";
        obj.ACTIVE = chkActive.Checked;
        obj.GROUPID = 0;
        DisplayInGrid(obj);
    }
    private void GetCostGroup()
    {
        try
        {
            CateBO cate = new CateBO();
            List<ZD_PRC_ZD_COST_GROUP_GETBYIDResult> lst = new List<ZD_PRC_ZD_COST_GROUP_GETBYIDResult>();
            lst = cate.CostGroupGet(-1);
            if (lst != null)
            {
                ddlGroupID.DataSource = lst;
                ddlGroupID.DataTextField = "TEN";
                ddlGroupID.DataValueField = "ID";
                ddlGroupID.DataBind();

                ListItem lstParent = new ListItem("--Chọn--", "0");
                ddlGroupID.Items.Insert(0, lstParent);
                ddlGroupID.SelectedIndex = ddlGroupID.Items.IndexOf(lstParent);
            }
        }
        catch
        {
        }
    }

    private void ClearTextBox()
    {
        hdfID.Value = "-1";
        txtName.Text = string.Empty;
        txtMa.Text = string.Empty;
        txtGhiChu.Text = string.Empty;
        chkActive.Checked = true;
        ddlGroupID.SelectedValue = "0";
        btnSave.Text = "Thêm mới";
        lblAlerting.Text = string.Empty;
    }
    protected void DisplayInGrid(ZD_CATE_COST obj)
    {
        CateBO ct = new CateBO();
        List<ZD_PRC_ZD_CATE_COST_SEARCHResult> lst = new List<ZD_PRC_ZD_CATE_COST_SEARCHResult>();
        lst = ct.CateCostSearch(obj).ToList();
        grdList.DataSource = lst;
        grdList.DataBind();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        hdfID.Value = "-1";
        lblAlerting.Text = string.Empty;
        LoadGrid();
    }

    protected void btnXoaTrang_Click(object sender, EventArgs e)
    {
        ClearTextBox();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (txtMa.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập mã chi phí!";
            return;
        }
        if (txtName.Text.Trim().Length <= 0)
        {
            lblAlerting.Text = "Bạn chưa nhập tên chi phí!";
            return;
        }
        if (int.Parse(ddlGroupID.SelectedValue)<= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn nhóm chi phí!";
            return;
        }



        // Thuc hien Insert Update
        ZD_CATE_COST obj = new ZD_CATE_COST();
        obj.ID = int.Parse(hdfID.Value);
        obj.NAME = txtName.Text.Trim();
        obj.GHICHU = txtGhiChu.Text.Trim();
        obj.UPDATEUSER = int.Parse(Session["UserID"].ToString());
        obj.CREATEUSER = int.Parse(Session["UserID"].ToString());
        obj.MA = txtMa.Text.Trim();
        obj.GROUPID = int.Parse(ddlGroupID.SelectedValue);
        obj.ACTIVE = chkActive.Checked;

        CateBO ct = new CateBO();
        if (int.Parse(hdfID.Value) <= 0)
        {
            hdfID.Value = ct.CateCostInsert(obj).ToString();
            if (int.Parse(hdfID.Value) > 0)
            {
                btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Thêm mới chi phí thành công!";
            }
            else
            {
                lblAlerting.Text = "Thêm mới chi phí thất bại, bạn vui lòng thử lại!";
            }
        }
        else
            if (ct.CateCostUpdate(obj))
            {
                //btnSave.Text = "Cập nhật";
                lblAlerting.Text = "Cập nhật chi phí thành công!";
            }
            else
            {
                lblAlerting.Text = "Cập nhật chi phí thất bại, bạn vui lòng thử lại!";
            }
        LoadGrid();

    }
    private void LoadGrid()
    {
        ZD_CATE_COST obj = new ZD_CATE_COST();
        obj.NAME = txtName.Text.Trim();
        obj.MA = txtMa.Text.Trim();
        obj.GHICHU = txtGhiChu.Text.Trim();
        obj.ACTIVE = chkActive.Checked;
        obj.GROUPID = int.Parse(ddlGroupID.SelectedValue);
        DisplayInGrid(obj);
    }



    protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadGrid();
        grdList.PageIndex = e.NewPageIndex;
        grdList.DataBind();
    }
    protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdList_RowEditing(object sender, GridViewEditEventArgs e)
    {

        lblAlerting.Text = string.Empty;
        btnSave.Text = "Cập nhật";
        grdList.EditIndex = e.NewEditIndex;
        hdfID.Value = grdList.DataKeys[e.NewEditIndex].Value.ToString();
        string strName = ((Label)grdList.Rows[e.NewEditIndex].FindControl("lblListingName")).Text;
        string strMa = ((Label)grdList.Rows[e.NewEditIndex].FindControl("lblListingMa")).Text;
        string strGhiChu = ((Label)grdList.Rows[e.NewEditIndex].FindControl("lblListingGhiChu")).Text;
        string strGroupID = ((Label)grdList.Rows[e.NewEditIndex].FindControl("lblListingGroupID")).Text;
        bool Active = bool.Parse(((Label)grdList.Rows[e.NewEditIndex].FindControl("lblListingActive")).Text);

        // Bind len control
        txtName.Text = strName;
        txtMa.Text = strMa;
        txtGhiChu.Text = strGhiChu;
        chkActive.Checked = Active;
        ddlGroupID.SelectedValue = strGroupID;
    }

}